package cn.ranko.shiro.utils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class EHCache {
	private static final CacheManager cacheManager = new CacheManager();  
    private Cache cache;  
    public EHCache(){  
        this.cache=cacheManager.getCache("ehcacheName");  
    }  
  
    public Cache getCache() {  
        return cache;  
    }  
  
    public void setCache(Cache cache) {  
        this.cache = cache;  
    }  
  
  
  
        /* 
     * 通过名称从缓存中获取数据 
     */  
    @SuppressWarnings("deprecation")
	public Object getCacheElement(String cacheKey) throws Exception {  
            net.sf.ehcache.Element e = cache.get(cacheKey);  
        if (e == null) {  
            return null;  
        }  
        return e.getValue();  
    }  
    /* 
     * 将对象添加到缓存中 
     */  
    public void addToCache(String cacheKey, Object result) throws Exception {  
        Element element = new Element(cacheKey, result);  
        cache.put(element);  
    }  
}
